package com.econ.powercloud.ems.controller;

import com.econ.powercloud.common.base.facade.pojo.MsgResult;
import com.econ.powercloud.common.base.facade.pojo.PageResult;
import com.econ.powercloud.ems.constant.SysConstant;
import com.econ.powercloud.ems.dal.entity.IotDeviceYkYtDit;
import com.econ.powercloud.ems.pojo.*;
import com.econ.powercloud.ems.pojo.requestVO.ClusterRelationPidRequestVO;
import com.econ.powercloud.ems.pojo.requestVO.ClusterSeatRequestVO;
import com.econ.powercloud.ems.pojo.requestVO.SingleListRequestVO;
import com.econ.powercloud.ems.pojo.requestVO.YkYtRangeRequestVO;
import com.econ.powercloud.ems.service.biz.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author : zjf
 * @Description : 设备controller
 * @Date : 2022/10/20 17:06
 * @Modified by : zjf
 */
@Api(tags = "设备管理")
@RestController
@RequestMapping(value = "/api/v2/device")
public class DeviceController extends ApiBaseController{

    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "设置电池簇的位置信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceCode", value = "设备编号", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "seat", value = "位置信息", required = false,dataType = "String"),
    })
    @PutMapping("clusterSeat")
    public MsgResult clusterSeat(@RequestBody @Validated ClusterSeatRequestVO requestVO){
        deviceService.clusterSet(requestVO.getDeviceCode(), requestVO.getSeat());
        return successHandler("设置成功！");
    }

    @ApiOperation(value = "批量自动设置所有电池簇的位置信息")
    @PutMapping("autoClusterSeatBatch")
    public MsgResult autoClusterSeatBatch(){
        deviceService.autoClusterSetBatch();
        return successHandler("设置成功！");
    }

    @ApiOperation(value = "电池监控的电池簇更新点号详情")
    @PutMapping("monitor/modifyClusterPid")
    public MsgResult modifyClusterPid(@RequestBody @Validated ClusterRelationPidRequestVO requestVO){
        deviceService.modifyClusterPid(requestVO);
        return successHandler("更新点号详情成功！");
    }

    @ApiOperation(value = "集装箱下的所有簇的展示信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceCode", value = "设备编号(集装箱/相)", dataType = "String")
    })
    @GetMapping("monitor/getContainerInfo")
    public MsgResult<List<ContainerKindVO>> getContainerInfo(@RequestParam("deviceCode")String deviceCode){
        return successHandler(deviceService.getContainerInfo(deviceCode));
    }

    @ApiOperation(value = "获取电池监控的电池簇点号详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceCode", value = "设备编号", dataType = "String")
    })
    @GetMapping("monitor/getClusterPidInfo")
    public MsgResult<List<FlashVO>> getClusterPidInfo(@RequestParam("deviceCode")String deviceCode){
        return successHandler(deviceService.getClusterPidInfo(deviceCode));
    }

    @ApiOperation(value = "初始化电池监控下的电池簇的点号配置基本信息(这个接口不对外开放,仅在初始化时操作)")
    @GetMapping(value = "initClusterRelationPid")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "stationId", value = "储能站id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "dits", value = "点号后缀集合,JSON格式(不要moduleCode)", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "ditNames", value = "点号名称集合,JSON格式(随便一个簇下就行)", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "modulePosition", value = "模块位置", dataType = "int"),
    })
    public String initClusterRelationPid(@RequestParam("stationId")String stationId,
                                         @RequestParam("dits")String dits,
                                         @RequestParam("ditNames")String ditNames,
                                         @RequestParam("modulePosition")Integer modulePosition){
        deviceService.initClusterRelationPid(stationId, dits, ditNames,modulePosition);
        return "初始化电池监控下的电池簇的点号配置基本信息成功";
    }

    @ApiOperation(value = "获取点号列表根据EMS设备编号与点号类型")
    @GetMapping(value = "getDitList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "deviceCode", value = "EMS设备编号", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "ditType", value = "点号类型", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "typeCode", value = "量测类型(只有遥测与遥信类型的有分类)", required = false,dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "search", value = "搜索名称", required = false,dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageNo", value = "页码", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页大小", defaultValue = "20", dataType = "int")
    })
    public MsgResult<PageResult<List<DeviceDitDataVO>>> getDitList(@RequestParam("deviceCode")String deviceCode,
                                                                   @RequestParam("ditType")Integer ditType,
                                                                   @RequestParam(value = "typeCode",required = false)Integer typeCode,
                                                                   @RequestParam(value ="search",required = false)String search,
                                                                   @RequestParam(value = "pageNo", defaultValue = SysConstant.DEFAULT_PAGENO) Integer pageNo,
                                                                   @RequestParam(value = "pageSize", defaultValue = SysConstant.DEFAULT_PAGESIZE) Integer pageSize){
        return successHandler(deviceService.getPageDeviceDit(deviceCode, ditType, typeCode, search, pageNo, pageSize));
    }

    @ApiOperation("获取点号的实时数据")
    @PostMapping("getFlashData")
    public MsgResult<List<FlashDataVO>> getFlashData(@RequestBody @Validated SingleListRequestVO<DitDataVO> requestVO) {
        return successHandler(deviceService.getFlashData(requestVO.getList()));
    }

    @ApiOperation("获取yt点号下发值范围")
    @PostMapping("getYtRange")
    public MsgResult<IotDeviceYkYtDit> getYtRange(@RequestBody YkYtRangeRequestVO requestVO){
        return successHandler(deviceService.getYkYtRange(requestVO));
    }

}
